package m.jclecai.com.adapter.KSPagerAdapters;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import m.jclecai.com.R;
import m.jclecai.com.inter.IAdapterItemSelect;

/**
 * Created by hp on 2017/5/2.
 * // 二同号
 */

public class BetTwoSameAdapter extends BaseAdapter implements View.OnClickListener {


    private String TAG = "一一一一一一一一一一一一一一一";
    private final IAdapterItemSelect iAadapterItemSelect;
    private final Context context;
    private String[] twoSame;
    private List<String> betParamete = new LinkedList<>(); //用户投注参数 快速选择
    Map<Integer, Boolean> selectMap = new HashMap<>(); //自选
    //随机数
    private int randomNumber;
    private boolean isClear;

    private int number;
    private ListView mListView;
    private boolean mShowMissData;
    private String[] mMissData=new String[]{};

    public BetTwoSameAdapter(Context context, IAdapterItemSelect iAadapterItemSelect) {
        this.context = context;
        this.iAadapterItemSelect = iAadapterItemSelect;
    }

    public void clearSelect() {
        isClear = true;
        number = 0;
    }

    public void setListView(ListView listView){
        mListView = listView;
    }

    public void setData(String[] twoSame) {
        this.twoSame = twoSame;
    }

    //获取投注参数
    public List<String> getBetParamete() {
        for (Map.Entry<Integer, Boolean> entry : selectMap.entrySet()) {
            if (entry.getValue()) {
                betParamete.add(entry.getKey() + "");
            }
        }
        return betParamete;
    }

    public void setRandomNumber(int randomNumber) {
       /* this.randomNumber = randomNumber;
        isClear = true;
        number = 0;
        */
        clearAll();
        List<String> stringList = Arrays.asList(twoSame);
        int numPosition = stringList.indexOf(randomNumber + "");
        int holderPostion = numPosition / 5;
        int itemPostion = numPosition % 5;

        View view = mListView.getChildAt(holderPostion);
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        switch (itemPostion) {
            case 0:
                setSelected(viewHolder, viewHolder.tv_number_one, viewHolder.rl_one);
                break;
            case 1:
                setSelected(viewHolder, viewHolder.tv_number_two, viewHolder.rl_two);
                break;
            case 2:
                setSelected(viewHolder, viewHolder.tv_number_three, viewHolder.rl_three);
                break;
            case 3:
                setSelected(viewHolder, viewHolder.tv_number_four, viewHolder.rl_four);
                break;
            case 4:
                setSelected(viewHolder, viewHolder.tv_number_five, viewHolder.rl_five);
                break;
        }
    }

    public void clearAll() {
        number=0;
        for (int i = 0; i < mListView.getChildCount(); i++) {
            View view = mListView.getChildAt(i);
            ViewHolder holder = (ViewHolder) view.getTag();
            clearSelect(holder);
        }
        isClear = false;
        iAadapterItemSelect.adapterItemSelect(selectMap);
    }

    @Override
    public int getCount() {
        return twoSame.length / 5 + 1;
    }

    @Override
    public Object getItem(int position) {
        return twoSame[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            holder.position = position;
            if (position == twoSame.length / 5) {
                convertView = View.inflate(context, R.layout.ks_item_two_same_bottom, null);
                holder.tv_select_one = (TextView) convertView.findViewById(R.id.tv_select_one);
                holder.tv_select_one.setOnClickListener(this);
                holder.tv_select_two = (TextView) convertView.findViewById(R.id.tv_select_two);
                holder.tv_select_two.setOnClickListener(this);
                holder.tv_select_three = (TextView) convertView.findViewById(R.id.tv_select_three);
                holder.tv_select_three.setOnClickListener(this);
                holder.tv_select_four = (TextView) convertView.findViewById(R.id.tv_select_four);
                holder.tv_select_four.setOnClickListener(this);
                holder.tv_select_five = (TextView) convertView.findViewById(R.id.tv_select_five);
                holder.tv_select_five.setOnClickListener(this);
                holder.tv_select_six = (TextView) convertView.findViewById(R.id.tv_select_six);
                holder.tv_select_six.setOnClickListener(this);

                holder.tv_select_one.setTag(holder);
                holder.tv_select_two.setTag(holder);
                holder.tv_select_three.setTag(holder);
                holder.tv_select_four.setTag(holder);
                holder.tv_select_five.setTag(holder);
                holder.tv_select_six.setTag(holder);

            } else {
                convertView = View.inflate(context, R.layout.ks_item_base, null);

                holder.rl_one = (RelativeLayout) convertView.findViewById(R.id.rl_one);
                holder.rl_one.setOnClickListener(this);
                holder.rl_two = (RelativeLayout) convertView.findViewById(R.id.rl_two);
                holder.rl_two.setOnClickListener(this);
                holder.rl_three = (RelativeLayout) convertView.findViewById(R.id.rl_three);
                holder.rl_three.setOnClickListener(this);
                holder.rl_four = (RelativeLayout) convertView.findViewById(R.id.rl_four);
                holder.rl_four.setOnClickListener(this);
                holder.rl_five = (RelativeLayout) convertView.findViewById(R.id.rl_five);
                holder.rl_five.setOnClickListener(this);

                holder.tv_miss_data_one = ((TextView) convertView.findViewById(R.id.tv_miss_data_one));
                holder.tv_miss_data_two = ((TextView) convertView.findViewById(R.id.tv_miss_data_two));
                holder.tv_miss_data_three = ((TextView) convertView.findViewById(R.id.tv_miss_data_three));
                holder.tv_miss_data_four = ((TextView) convertView.findViewById(R.id.tv_miss_data_four));
                holder.tv_miss_data_five = ((TextView) convertView.findViewById(R.id.tv_miss_data_five));


                holder.rl_one.setTag(holder);
                holder.rl_two.setTag(holder);
                holder.rl_three.setTag(holder);
                holder.rl_four.setTag(holder);
                holder.rl_five.setTag(holder);

                holder.tv_number_one = (TextView) convertView.findViewById(R.id.tv_number_one);
                holder.tv_number_two = (TextView) convertView.findViewById(R.id.tv_number_two);
                holder.tv_number_three = (TextView) convertView.findViewById(R.id.tv_number_three);
                holder.tv_number_four = (TextView) convertView.findViewById(R.id.tv_number_four);
                holder.tv_number_five = (TextView) convertView.findViewById(R.id.tv_number_five);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置数据
        switch (position) {
            case 0:
                Log.e(TAG, "twoSame=" + twoSame[0]);
                holder.tv_number_one.setText(twoSame[0]);
                holder.tv_number_two.setText(twoSame[1]);
                holder.tv_number_three.setText(twoSame[2]);
                holder.tv_number_four.setText(twoSame[3]);
                holder.tv_number_five.setText(twoSame[4]);

                if (mMissData.length>=5) {
                    holder.tv_miss_data_one.setText(mMissData[0]);
                    holder.tv_miss_data_two.setText(mMissData[1]);
                    holder.tv_miss_data_three.setText(mMissData[2]);
                    holder.tv_miss_data_four.setText(mMissData[3]);
                    holder.tv_miss_data_five.setText(mMissData[4]);
                }
                break;
            case 1:
                holder.tv_number_one.setText(twoSame[5]);
                holder.tv_number_two.setText(twoSame[6]);
                holder.tv_number_three.setText(twoSame[7]);
                holder.tv_number_four.setText(twoSame[8]);
                holder.tv_number_five.setText(twoSame[9]);
                if (mMissData.length>=10) {
                    holder.tv_miss_data_one.setText(mMissData[5]);
                    holder.tv_miss_data_two.setText(mMissData[6]);
                    holder.tv_miss_data_three.setText(mMissData[7]);
                    holder.tv_miss_data_four.setText(mMissData[8]);
                    holder.tv_miss_data_five.setText(mMissData[9]);
                }
                break;
            case 2:
                holder.tv_number_one.setText(twoSame[10]);
                holder.tv_number_two.setText(twoSame[11]);
                holder.tv_number_three.setText(twoSame[12]);
                holder.tv_number_four.setText(twoSame[13]);
                holder.tv_number_five.setText(twoSame[14]);

                if (mMissData.length>=15) {
                    holder.tv_miss_data_one.setText(mMissData[10]);
                    holder.tv_miss_data_two.setText(mMissData[11]);
                    holder.tv_miss_data_three.setText(mMissData[12]);
                    holder.tv_miss_data_four.setText(mMissData[13]);
                    holder.tv_miss_data_five.setText(mMissData[14]);
                }
                break;
            case 3:
                holder.tv_number_one.setText(twoSame[15]);
                holder.tv_number_two.setText(twoSame[16]);
                holder.tv_number_three.setText(twoSame[17]);
                holder.tv_number_four.setText(twoSame[18]);
                holder.tv_number_five.setText(twoSame[19]);

                if (mMissData.length>=20) {
                    holder.tv_miss_data_one.setText(mMissData[15]);
                    holder.tv_miss_data_two.setText(mMissData[16]);
                    holder.tv_miss_data_three.setText(mMissData[17]);
                    holder.tv_miss_data_four.setText(mMissData[18]);
                    holder.tv_miss_data_five.setText(mMissData[19]);
                }
                break;
            case 4:
                holder.tv_number_one.setText(twoSame[20]);
                holder.tv_number_two.setText(twoSame[21]);
                holder.tv_number_three.setText(twoSame[22]);
                holder.tv_number_four.setText(twoSame[23]);
                holder.tv_number_five.setText(twoSame[24]);
                if (mMissData.length>=25) {
                    holder.tv_miss_data_one.setText(mMissData[20]);
                    holder.tv_miss_data_two.setText(mMissData[21]);
                    holder.tv_miss_data_three.setText(mMissData[22]);
                    holder.tv_miss_data_four.setText(mMissData[23]);
                    holder.tv_miss_data_five.setText(mMissData[24]);
                }
                break;
            case 5:
                holder.tv_number_one.setText(twoSame[25]);
                holder.tv_number_two.setText(twoSame[26]);
                holder.tv_number_three.setText(twoSame[27]);
                holder.tv_number_four.setText(twoSame[28]);
                holder.tv_number_five.setText(twoSame[29]);
                if (mMissData.length>=30) {
                    holder.tv_miss_data_one.setText(mMissData[25]);
                    holder.tv_miss_data_two.setText(mMissData[26]);
                    holder.tv_miss_data_three.setText(mMissData[27]);
                    holder.tv_miss_data_four.setText(mMissData[28]);
                    holder.tv_miss_data_five.setText(mMissData[29]);
                }
                break;
        }

        if(position<getCount()-1){
            if (isShowMissData()) {
                holder.tv_miss_data_one.setVisibility(View.VISIBLE);
                holder.tv_miss_data_two.setVisibility(View.VISIBLE);
                holder.tv_miss_data_three.setVisibility(View.VISIBLE);
                holder.tv_miss_data_four.setVisibility(View.VISIBLE);
                holder.tv_miss_data_five.setVisibility(View.VISIBLE);
            } else {
                holder.tv_miss_data_one.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_two.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_three.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_four.setVisibility(View.INVISIBLE);
                holder.tv_miss_data_five.setVisibility(View.INVISIBLE);
            }
        }

        if (isClear) {
            clearSelect(holder);
        }

        //随机
        if (randomNumber != 0 && position < twoSame.length / 5) {

            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_four.setBackgroundResource(R.drawable.shape_box_ks_up);
            if (randomNumber == Integer.parseInt(holder.tv_number_one.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_one, holder.rl_one);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_two.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_two, holder.rl_two);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_three.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_three, holder.rl_three);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_four.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_four, holder.rl_four);
            } else if (randomNumber == Integer.parseInt(holder.tv_number_five.getText().toString())) {
                speedinessSelected(holder, holder.tv_number_five, holder.rl_five);
            }

            if (position == twoSame.length / 5 -1) {
                randomNumber = 0;
                isClear = false;
            }
        }

        return convertView;
    }

    private void clearSelect(ViewHolder holder) {
        if (number == 0) {
            selectMap.clear();
            betParamete.clear();
            number++;
        }


        if (holder.position < twoSame.length / 5) {
            holder.rl_one.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_two.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_three.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_four.setBackgroundResource(R.drawable.shape_box_ks_up);
            holder.rl_five.setBackgroundResource(R.drawable.shape_box_ks_up);

            //设置颜色为白的色
            holder.tv_number_one.setTextColor(holder.rl_four.getResources().getColor(R.color.white));
            holder.tv_number_two.setTextColor(holder.rl_four.getResources().getColor(R.color.white));
            holder.tv_number_three.setTextColor(holder.rl_four.getResources().getColor(R.color.white));
            holder.tv_number_four.setTextColor(holder.rl_four.getResources().getColor(R.color.white));
            holder.tv_number_five.setTextColor(holder.rl_four.getResources().getColor(R.color.white));

        } else {
            clearSelect(holder.tv_select_one);
            clearSelect(holder.tv_select_two);
            clearSelect(holder.tv_select_three);
            clearSelect(holder.tv_select_four);
            clearSelect(holder.tv_select_five);
            clearSelect(holder.tv_select_six);
            isClear = false;
            iAadapterItemSelect.adapterItemSelect(selectMap);
        }
    }


    //快速选号
    private void speedinessSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        rl.setBackgroundResource(R.drawable.shape_box_ks_down);
        tv.setTextColor(tv.getResources().getColor(R.color.yello));
        holder.setSelected(key, true);
    }


    int key; //键名

    private void setSelected(ViewHolder holder, TextView tv, RelativeLayout rl) {
        key = 0;
        key = Integer.parseInt(tv.getText().toString());
        if (holder.isSelected(key)) {
            if (rl != null) {
                rl.setBackgroundResource(R.drawable.shape_box_ks_up);
                tv.setTextColor(tv.getResources().getColor(R.color.white));
            } else {
                clearSelect(tv);
            }
            holder.removeSelected(key);
        } else {
            if (rl != null) {
                rl.setBackgroundResource(R.drawable.shape_box_ks_down);
                //这里是还需要修改其颜色的；
                tv.setTextColor(tv.getResources().getColor(R.color.yello));
            } else {
                selectMode(tv);
            }
            holder.setSelected(key, true);
        }
    }

    @Override
    public void onClick(View v) {
        ViewHolder holder = (ViewHolder) v.getTag();
        switch (v.getId()) {
            case R.id.tv_select_one:
                setSelected(holder, holder.tv_select_one, null);
                break;
            case R.id.tv_select_two:
                setSelected(holder, holder.tv_select_two, null);
                break;
            case R.id.tv_select_three:
                setSelected(holder, holder.tv_select_three, null);
                break;
            case R.id.tv_select_four:
                setSelected(holder, holder.tv_select_four, null);
                break;
            case R.id.tv_select_five:
                setSelected(holder, holder.tv_select_five, null);
                break;
            case R.id.tv_select_six:
                setSelected(holder, holder.tv_select_six, null);
                break;
            case R.id.rl_one:
                setSelected(holder, holder.tv_number_one, holder.rl_one);
                break;
            case R.id.rl_two:
                setSelected(holder, holder.tv_number_two, holder.rl_two);
                break;
            case R.id.rl_three:
                setSelected(holder, holder.tv_number_three, holder.rl_three);
                break;
            case R.id.rl_four:
                setSelected(holder, holder.tv_number_four, holder.rl_four);
                break;
            case R.id.rl_five:
                setSelected(holder, holder.tv_number_five, holder.rl_five);
                break;
        }
       // notifyDataSetChanged();
    }

    //设置选中
    private void selectMode(TextView tv) {
       // tv.setBackgroundResource(R.color.yello);
        ((ViewGroup) tv.getParent()).setBackgroundResource(R.drawable.shape_box_ks_down);
        tv.setTextColor(tv.getResources().getColor(R.color.yello));
    }

    //清除选中
    private void clearSelect(TextView tv) {
        //tv.setBackgroundResource(R.color.green_1);
        ((ViewGroup) tv.getParent()).setBackgroundResource(R.drawable.shape_box_ks_up);
        tv.setTextColor(tv.getResources().getColor(R.color.white));
    }


    private class ViewHolder {

        int position;

        RelativeLayout rl_one;
        RelativeLayout rl_two;
        RelativeLayout rl_three;
        RelativeLayout rl_four;
        RelativeLayout rl_five;

        TextView tv_number_one;
        TextView tv_number_two;
        TextView tv_number_three;
        TextView tv_number_four;
        TextView tv_number_five;

        TextView tv_miss_data_one;
        TextView tv_miss_data_two;
        TextView tv_miss_data_three;
        TextView tv_miss_data_four;
        TextView tv_miss_data_five;

        TextView tv_select_one;
        TextView tv_select_two;
        TextView tv_select_three;
        TextView tv_select_four;
        TextView tv_select_five;
        TextView tv_select_six;

        public void setSelected(int key, boolean value) {
            selectMap.put(key, value);
            Log.i(TAG, "添加后=" + selectMap.size());
            iAadapterItemSelect.adapterItemSelect(selectMap);
        }

        public void removeSelected(int key) {
            selectMap.remove(key);
            Log.i(TAG, "删除后=" + selectMap.size());
            iAadapterItemSelect.adapterItemSelect(selectMap);
        }

        public boolean isSelected(int key) {
            if (selectMap.get(key) == null) {
                return false;
            } else {
                return selectMap.get(key);
            }
        }
    }

    public boolean isShowMissData(){
        return mShowMissData;

    }
    public void addMissDataInfo(String missData){
        mMissData = missData.split(",");
        //这里我们要做的一个操作就是显示将数据添加到里面去；

        if(mListView.getChildCount()==0){
            notifyDataSetChanged();
        }else{
            if(mMissData.length>=30){
                for (int i = 0; i < mListView.getChildCount()-1; i++) {
                    ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();
                    //没一个条目有五个子view;
                    viewHolder.tv_miss_data_one.setText(mMissData[i*5+0]);
                    viewHolder.tv_miss_data_two.setText(mMissData[i*5+1]);
                    viewHolder.tv_miss_data_three.setText(mMissData[i*5+2]);
                    viewHolder.tv_miss_data_four.setText(mMissData[i*5+3]);
                    viewHolder.tv_miss_data_five.setText(mMissData[i*5+4]);
                }
            }else{
                for (int i = 0; i < mListView.getChildCount()-1; i++) {
                    ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();
                    //没一个条目有五个子view;
                    viewHolder.tv_miss_data_one.setText("--");
                    viewHolder.tv_miss_data_two.setText("--");
                    viewHolder.tv_miss_data_three.setText("--");
                    viewHolder.tv_miss_data_four.setText("--");
                    viewHolder.tv_miss_data_five.setText("--");
                }
            }
        }
    }


    public void showMissData(){
        mShowMissData=true;

        if(mListView.getChildCount()==0){
            notifyDataSetChanged();

        }else{
            for (int i = 0; i < mListView.getChildCount()-1; i++) {
                ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();

                viewHolder.tv_miss_data_one.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_two.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_three.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_four.setVisibility(View.VISIBLE);
                viewHolder.tv_miss_data_five.setVisibility(View.VISIBLE);
            }

        }
    }
    public void hideMissData(){
        mShowMissData=false;
        int childCount = mListView.getChildCount();
        if(childCount==0){
            notifyDataSetChanged();
        }else{

            for (int i = 0; i < mListView.getChildCount()-1; i++) {
                ViewHolder viewHolder = (ViewHolder) mListView.getChildAt(i).getTag();

                viewHolder.tv_miss_data_one.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_two.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_three.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_four.setVisibility(View.INVISIBLE);
                viewHolder.tv_miss_data_five.setVisibility(View.INVISIBLE);
            }
        }
    }



}
